package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.R;
import com.fy.fyspace.service.FavoritePictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import static com.fy.fyspace.common.result.R.no;

/**
 * <p>
 * 图片 前端控制器
 * </p>
 *
 * @author fwq
 * @since 2025-06-25
 */
@Slf4j
@Api(value = "收藏图片相关接口", tags = "收藏图片")
@RestController
@RequestMapping("/favoritepicture")
public class FavoritePictureController {

    @Resource
    private FavoritePictureService favoritePictureService;
    /**
     * 批量收藏图片
     * @param picIds 图片ID列表，逗号分隔
     * @return 结果
     */
    @ApiOperation(value = "批量收藏图片", notes = "批量收藏图片，picIds为逗号分隔的图片ID字符串")
    @PostMapping("/favorite/batch")
    public R batchFavoritePictures(
            @ApiParam(value = "图片ID列表，逗号分隔", required = true)
            @RequestParam String picIds) {
        return favoritePictureService.batchFavoritePictures(picIds);
    }

    /**
     * 批量取消收藏图片
     * @param picIds 图片ID列表，逗号分隔
     * @return 结果
     */
    @ApiOperation(value = "批量取消收藏图片", notes = "批量取消收藏图片，picIds为逗号分隔的图片ID字符串")
    @PostMapping("/cancel/batch")
    public R batchCancelFavoritePictures(
            @ApiParam(value = "图片ID列表，逗号分隔", required = true)
            @RequestParam String picIds) {
        return favoritePictureService.batchCancelFavoritePictures(picIds);
    }

    /**
     * 获取我的收藏图片列表
     * @param pageNo 页码
     * @param pageSize 每页数量
     * @param session HttpSession
     * @return 分页图片列表，包含点赞/收藏数
     */
    @ApiOperation(value = "获取我的收藏图片列表", notes = "分页获取当前用户收藏的图片列表，无缓存")
    @GetMapping("/list")
    @GlobalInterceptor
    public R getMyFavoritePictureList(@RequestParam(required = false) Integer pageNo,
                                           @RequestParam(required = false) Integer pageSize,
                                           @RequestParam(required = false) String searchText,
                                           @RequestParam(required = false) String color,
                                           HttpSession session) {
        com.fy.fyspace.model.vo.user.UserSessionVO userSessionVO = (com.fy.fyspace.model.vo.user.UserSessionVO) session.getAttribute(com.fy.fyspace.constant.KeysConstant.LOGIN_SESSION_KEY);
        if (userSessionVO == null) {
            return no("未登录");
        }
        return favoritePictureService.getMyFavoritePictureList(userSessionVO.getId(), pageNo, pageSize,searchText, color);
    }
}
